<template>
  <div class="login">
    <el-form :model="form" class="form" :rules="rules" ref="formdata">
      <div class="img">
        <img src="../../assets/common/login-logo.png" alt="" />
      </div>
      <el-form-item prop="name">
        <el-input
          prefix-icon="el-icon-user-solid"
          v-model="form.name"
          placeholder="username"
        ></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          prefix-icon="el-icon-lock"
          type="password"
          v-model="form.password"
          placeholder="password"
          show-password
        ></el-input>
      </el-form-item>
      <el-form-item v-loading="loading">
        <el-button class="btn" @click="submit">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { mapActions } from 'vuex'
export default {
  name: 'login',
  data () {
    return {
      loading: false,
      form: {
        name: '13800000002',
        password: '123456'
      },
      rules: {
        name: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          {
            pattern: /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/,
            message: '用户名格式错误',
            trigger: 'blur'
          }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          {
            min: 6,
            message: '密码最少6位数',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    ...mapActions(['user/login']),
    submit () {
      this.$refs.formdata.validate(async result => {
        try {
          this.loading = true
          await this['user/login']({
            mobile: this.form.name,
            password: this.form.password
          })
          this.loading = false
          this.$router.push('/')
        } catch {
          console.log('登录失败')
        } finally {
          this.loading = false
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.login {
  height: 100%;
  background-image: url('../../assets/common/login.jpg');
  background-position: center;
  position: relative;
  ::v-deep .form {
    width: 440px;
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    margin: 230px auto 0;
    position: fixed;
    justify-content: center;
    .img {
      margin-bottom: 45px;
    }
    .el-input__inner {
      height: 50px;
      padding: 10px 40px;
      background-color: #d4e3ff;
    }
    .el-button {
      width: 440px;
      height: 60px;
      background-color: #407ffe;
      border: 0;
      font-size: 25px;
      color: #fff;
    }
    .el-form-item__error {
      color: #fff;
    }
  }
}
</style>
